package frmwrk.db.publicInterface;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import utils.Tracer;
import frmwrk.db.internal.classes.Session;

public class ConnectionManager {
	
	private static String connString = "jdbc:h2:~/Dropbox/Java/Deutsch/DB/db;schema=user";
	private static String username = "deutsch";
	private static String password = "orion";
	private static Connection conn;
	
	public static ISession getSession(){
    ISession newSession = null;
		try {
    	newSession = new Session(getConnection().createStatement());
    } catch (SQLException e) {
  		Tracer.trace("ConnectionManager.Error creating statement", e);
    }
		return newSession;
	}
	
	private static Connection getConnection(){
		if (conn == null)
			connect();
		return conn;
	}
	
	public static void connect() {
		try {
			Tracer.trace("ConnectionManager.Connecting");
			Class.forName("org.h2.Driver");
			conn = DriverManager.getConnection(connString, username, password);	

			if (conn == null)
				Tracer.trace("ConnectionManager.Connection failed!");
			else
				Tracer.trace("ConnectionManager.Connected!");
		} catch (Exception e) {
			Tracer.trace(e);
		}  
	}

	public void disconnect() {
		try {
			conn.close();
		} catch (SQLException e) {
			Tracer.trace(e);
		}			
	}
  
	

}
